package utils

import (
	"fmt"
	ants "github.com/panjf2000/ants/v2"
	"runtime/debug"
	"sync"
)

var (
	poolMap = sync.Map{}
)

func GetPoolV2(poolName string, size int, callback func(data interface{})) *ants.PoolWithFunc {
	if pool, ok := poolMap.Load(poolName); ok {
		return pool.(*ants.PoolWithFunc)
	}

	// 协程池  异步调用统计方法,每一手牌结算
	pool, _ := ants.NewPoolWithFunc(size, func(data interface{}) {
		defer func() {
			if p := recover(); p != nil {
				fmt.Printf("threading recover error is \n %+v\n %s", p, debug.Stack())
			}
		}()

		callback(data)
	})

	poolMap.Store(poolName, pool)
	return pool
}
